package com.kylin.upms.biz.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kylin.upms.biz.dto.UserDto;
import com.kylin.upms.biz.entity.User;
import com.kylin.upms.biz.exception.UserException;
import com.kylin.upms.biz.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kylin.common.result.ResEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2019-04-11
 */
@RestController
@RequestMapping("/user")
@Api("用户接口文档说明")
public class UserController {

    @Autowired
    IUserService userService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation("分条件查询用户列表")
    public ResEntity getUserList(UserDto userDto){
        try {
            Page<User> page = new Page<User>(userDto.getPageNo(),userDto.getPageSize());
            User user = new User();
            BeanUtils.copyProperties(userDto,user);
            EntityWrapper<User> entityWrapper = new EntityWrapper<User>(user);
            Page<User> page1 = userService.selectPage(page, entityWrapper);
            return ResEntity.ok(page1);
        }catch (Exception e){
            throw new UserException(e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST)
    public ResEntity save(UserDto userDto){
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        userService.saveUser(user);
        return ResEntity.ok("新增成功");
    }


}
